package com.pucheng.bracelet.app.util

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import com.blankj.utilcode.util.LogUtils
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.App
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.min


/**
 * 作者　: hegaojian
 * 时间　: 2020/3/10
 * 描述　: 时间工具类
 */

object DatetimeUtil {

    const val  DATE_MD = "MM/dd"
    const val  DATE_EE = "EEEE"
    const val  DATE_PATTERN = "yyyy-MM-dd"
    const val  DATE_PATTERN_SS = "yyyy-MM-dd HH:mm:ss"
    const val  DATE_PATTERN_A_H = "yyyy-MM-dd a HH:mm"
    const val  DATE_PATTERN_MM = "yyyy-MM-dd HH:mm"
    const val  DATE_PATTERN_EE = "yyyy-MM-dd EEEE"
    const val  DATE_PATTERN_HHMM = "HH:mm"

    /**
     * 获取现在时刻
     */
    val now: Date
        get() = Date(Date().time)

    /**
     * 获取现在时刻
     */
    val nows: Date
        get() = formatDate(DATE_PATTERN, now)


    /**
     * Date to Strin
     */
    @SuppressLint("SimpleDateFormat")
    @JvmStatic
    fun formatDate(date: Date?, formatStyle: String): String {
        return if (date != null) {
            val sdf = SimpleDateFormat(formatStyle)
            sdf.format(date)
        } else {
            ""
        }

    }

    /**
     * Date to Strin
     */
    @SuppressLint("SimpleDateFormat")
    @JvmStatic
    fun formatDate(date: Long, formatStyle: String): String {

        var dateLong = date
        if (dateLong < 10000000000) {
            dateLong *= 1000
        }
        val sdf = SimpleDateFormat(formatStyle)
        return sdf.format(Date(dateLong))

    }

    @JvmStatic
    fun formatDate(formatStyle: String, formatStr: String): Date {
        val format = SimpleDateFormat(formatStyle)
        return try {
            val date = Date()
            date.time = format.parse(formatStr).time
            date
        } catch (e: Exception) {
            println(e.message)
            nows
        }

    }

    /**
     * Date to Date
     */
    @SuppressLint("SimpleDateFormat")
    @JvmStatic
    fun formatDate(formatStyle: String, date: Date?): Date {
        if (date != null) {
            val sdf = SimpleDateFormat(formatStyle)
            val formatDate = sdf.format(date)
            try {
                return sdf.parse(formatDate)
            } catch (e: ParseException) {
                e.printStackTrace()
                return Date()
            }

        } else {
            return Date()
        }
    }

    /**
     * 将时间戳转换为时间
     */
    @JvmStatic
    fun stampToDate(s: String): Date {
        val lt = s.toLong()
        return Date(lt)
    }

    /**
     * 获得指定时间的日期
     */
    @JvmStatic
    fun getCustomTime(dateStr: String): Date {
        return formatDate(DATE_PATTERN, dateStr)
    }

    @JvmStatic
    fun getWeekIndexToDay(): Int {
        return Calendar.getInstance()[Calendar.DAY_OF_WEEK]
    }

    @JvmStatic
    fun getWeekToDay(): String? {
        val c = Calendar.getInstance()
        val mYear = java.lang.String.valueOf(c[Calendar.YEAR]) // 获取当前年份
        val mMonth = java.lang.String.valueOf(c[Calendar.MONTH] + 1) // 获取当前月份
        val mDay = java.lang.String.valueOf(c[Calendar.DAY_OF_MONTH]) // 获取当前月份的日期号码
        var mWay = java.lang.String.valueOf(c[Calendar.DAY_OF_WEEK])
        if ("1" == mWay) {
            mWay = "天"
        } else if ("2" == mWay) {
            mWay = "一"
        } else if ("3" == mWay) {
            mWay = "二"
        } else if ("4" == mWay) {
            mWay = "三"
        } else if ("5" == mWay) {
            mWay = "四"
        } else if ("6" == mWay) {
            mWay = "五"
        } else if ("7" == mWay) {
            mWay = "六"
        }
        return "$mYear.$mMonth.$mDay  周$mWay"
    }

    @JvmStatic
    fun getWeekRepeatData(weekChecks: Int): String {
//        val weekChecks = CacheUtil.getWeekChecks()
        if (weekChecks != 0) {
            if (weekChecks == 127) {
                return App.instance.getString(R.string.allday)
            }
            val weekDatas = App.instance.resources.getStringArray(R.array.week)
            var weekRepeat = StringBuffer()
            for (i in 0..6) {
                //6543217
                if (ByteUtils.getIntToIAsBit(weekChecks, i)) {
                    weekRepeat.append(weekDatas[i] + ",")
                }
            }
            return if (TextUtils.isEmpty(weekRepeat.toString())) {
                App.instance.resources.getString(R.string.single)
            } else {
                weekRepeat.substring(0, weekRepeat.length - 1).toString()
            }
        }else {
            return App.instance.resources.getString(R.string.no_repeat)
        }
    }

    @JvmStatic
    fun getNowDate(formatStr: String): String {
        val simpleDateFormat =
            SimpleDateFormat(formatStr) // HH:mm:ss
        //获取当前时间
        //获取当前时间
        val date = Date(System.currentTimeMillis())
        return simpleDateFormat.format(date)
    }

    @JvmStatic
    fun getOldDate(beginDate: Date,distanceDay: Int): String {
        val dft = SimpleDateFormat(DATE_PATTERN)
        val date = Calendar.getInstance()
        date.time = beginDate
        date[Calendar.DATE] = date[Calendar.DATE] + distanceDay
        var endDate: Date? = null
        try {
            endDate = dft.parse(dft.format(date.time))
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        LogUtils.d("当前日期==" + dft.format(endDate))
        return dft.format(endDate)
    }

    @JvmStatic
    fun getOldDate(distanceDay: Int): String {
        val dft = SimpleDateFormat(DATE_PATTERN)
        val beginDate = Date()
        val date = Calendar.getInstance()
        date.time = beginDate
        date[Calendar.DATE] = date[Calendar.DATE] + distanceDay
        var endDate: Date? = null
        try {
            endDate = dft.parse(dft.format(date.time))
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        LogUtils.d("当前日期==" + dft.format(endDate))
        return dft.format(endDate)
    }

    /**
     * 分钟数转化HH小时MM分钟格式
     */
    @JvmStatic
    fun minToHourAndMin(min:Int):String{
        val hour: Int = min / 60
        val min: Int = min - hour * 60
        return hour.toString() + App.instance.getString(R.string.hour) + min + App.instance.getString(R.string.minute)
    }

}
